package com.naxian.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.naxian.mapper.CompanyMapper;
import com.naxian.mapper.RecruitMapper;
import com.naxian.pojo.Company;
import com.naxian.pojo.CompanyExample;
import com.naxian.pojo.Recruit;
import com.naxian.pojo.RecruitExample;
import com.naxian.service.searchInfo.SearchInfoService;
import com.naxian.vo.SearchInfoDTO;
import com.naxian.vo.SearchInfoResultDTO;
import com.naxian.vo.search.SearchCenterVo;
import com.naxian.vo.searchInfo.SearchInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.REException;

import java.util.ArrayList;
import java.util.List;
@Service
public class SearchInfoServiceImpl implements SearchInfoService {
    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private RecruitMapper recruitMapper;
//根据多条件获得对应的
    @Override
    public PageInfo<SearchInfoDTO> query(Integer page,SearchInfoResultDTO searchInfoResultDTO){
            PageHelper.startPage(page,16);
            List<SearchInfoDTO> searchInfoDTOS = recruitMapper.searchByMoreCondition(searchInfoResultDTO);
            PageInfo<SearchInfoDTO> pageInfo = new PageInfo<>(searchInfoDTOS);
            return pageInfo;
    }


    @Override
    public PageInfo getByInfo(int pageNum, int pagetSize) {
        SearchInfo searchInfo =new SearchInfo();
        Company company =new Company();
        RecruitExample recruitExample = new RecruitExample();
        CompanyExample companyExample =new CompanyExample();
        CompanyExample.Criteria criteria = companyExample.createCriteria();
        RecruitExample.Criteria recruitCriteria = recruitExample.createCriteria();
        Recruit recruit =new Recruit();
        PageHelper.startPage(pageNum,pagetSize);
        List<Company> companyList = companyMapper.selectByExample(null);
        List<Recruit>recruitList =new ArrayList<>();
        PageInfo pageInfo = new PageInfo(companyList);
        List<SearchInfo> searchInfos = new ArrayList<>();
        for (Company c:companyList
             ) {
            int companyId = c.getCompanyId();
                  criteria.andCompanyIdEqualTo(companyId);
            if (searchInfo.getCompanyList()!= null &&!searchInfo.getCompanyList().equals(""))
                criteria.andCompanySizeEqualTo(company.getCompanySize());
                criteria.andCompanyTypeEqualTo(company.getCompanyType());
            if (searchInfo.getRecruitList()!= null &&!searchInfo.getRecruitList().equals(""))
                recruitCriteria.andRecJobSalaryEqualTo(recruit.getRecJobSalary());
                recruitCriteria.andEducationEqualTo(recruit.getEducation());
             companyList = companyMapper.selectByExample(companyExample);
            searchInfo.setCompanyList(companyList);
            searchInfo.setRecruitList(recruitList);
            searchInfos.add(searchInfo);
        }
        pageInfo.setList(searchInfos);
        System.out.println("////////"+pageInfo);
        return pageInfo;
    }
    //根据条件查询公司表和招聘表 获取一个list
    @Override
    public List<SearchCenterVo> getByStyle(String salary, String companyType, String education, String companySize) {
        System.out.println("service开始");
        List<SearchCenterVo> searchCenterVoList = new ArrayList<>();
        SearchCenterVo searchCenterVo = null;
        CompanyExample companyExample =new CompanyExample();
        CompanyExample.Criteria companyCriteria = companyExample.createCriteria();
        companyCriteria.andCompanyTypeEqualTo(companyType);
        companyCriteria.andCompanySizeEqualTo(companySize);
        List<Company> companyList = companyMapper.selectByExample(companyExample);
        for (Company c:companyList){
            int comId = c.getCompanyId();
            Company company = companyMapper.selectByPrimaryKey(comId);
            RecruitExample recruitExample = new RecruitExample();
            RecruitExample.Criteria recruitCriteria = recruitExample.createCriteria();
            recruitCriteria.andEducationEqualTo(education);
            recruitCriteria.andRecJobSalaryEqualTo(salary);
            List<Recruit> recruitList = recruitMapper.selectByExample(recruitExample);
            for (Recruit r:recruitList){
                int recId = r.getRecId();
                Recruit recruit = recruitMapper.selectByPrimaryKey(recId);
                searchCenterVo = new SearchCenterVo(company,recruit);
                searchCenterVoList.add(searchCenterVo);
            }
        }
        System.out.println("service结束");
        return searchCenterVoList;
    }
    //获取数据库中所有招聘的总数
    @Override
    public int getByStyle() {
        List<Recruit> recruitList = recruitMapper.selectByExample(null);
        int count = recruitList.size();
        return count;
    }

}
